# -*- coding:utf-8 -*-

# @Time : 2023/4/19 16:08
# @Author : 快乐的小猴子
# @Version : 
# @Function : 
import requests
import time
import os
from lxml import etree
from LvMaMa.conf import settings


class Commants():

    def send(self, url):
        resp = requests.get(url, headers=settings.headers).text
        # resp = requests.get(url, headers=setting.headers, proxies=SetProxies().get_proxies()).text
        return resp

    def get_hp_infos(self, resp):
        tree = etree.HTML(resp)
        spot_name = tree.xpath('//div[@class="product-list"]//div[@class="product-section"]/h3/a/text()')
        spot_url = tree.xpath('//div[@class="product-list"]//div[@class="product-section"]/h3/a/@href')
        page_cnts = tree.xpath('//div[@class="pagebox"]/a/text()')
        return spot_name, spot_url, page_cnts

    def get_sec_data(self, names, urls):
        resp_dics = {}
        # for i in range(0, len(names)):
        for i in range(0, 2):
            resp_cont = self.send(urls[i])
            resp_dics[names[i]] = resp_cont
        return resp_dics

    def get_comm_datas(self, resp_dics):
        all_spot_comms = {}
        for k, v in resp_dics.items():
            one_spot_comms = []
            tree = etree.HTML(v)
            comm_cont = tree.xpath('//div[@class="comment-list com-all"]/div[@class="comment-li"]/div[2]/text()')
            for i in range(0, len(comm_cont)):
                if comm_cont[i].strip():
                    one_spot_comms.append(comm_cont[i].strip())
            all_spot_comms[k] = one_spot_comms
        return all_spot_comms

    def verf_dir(self):
        file_path = os.getcwd() + '\\file\\'
        if not os.path.exists(file_path):
            os.makedirs(file_path)
        return file_path

    def save_file(self, name, spot_name, all_spot_comms):
        file_path = self.verf_dir()
        with open('{}{}各景点评论信息.csv'.format(file_path, name), 'a', encoding='utf-8') as fp:
            # for i in range(0, len(spot_name)):
            for i in range(0, 2):
                # print(spot_name[i], all_spot_comms[spot_name[i]])
                fp.write('【' + spot_name[i] + '】景点所有的评论信息：\n')
                for j in range(0, len(all_spot_comms[spot_name[i]])):
                    print(j + 1, all_spot_comms[spot_name[i]][j])
                    fp.write(all_spot_comms[spot_name[i]][j] + '\n')
        print('保存完成~~~')

    def comm_main(self):
        while True:
            flag = input('是否继续查询数据？(y/n)')
            if flag.strip().upper() == 'N':
                break
            else:
                name = input('请输入您要查询景点门票的城市(q/Q退出程序)：')
                # for num in range(1, 12):
                for num in range(1, 2):
                    url_new = settings.url.format(str(num), name)
                    resp = self.send(url_new)
                    time.sleep(0.25)
                    spot_name, spot_url, page_cnts = self.get_hp_infos(resp)
                    print(spot_name, spot_url, page_cnts)
                    resp_dics = self.get_sec_data(spot_name, spot_url)
                    all_spot_comms = self.get_comm_datas(resp_dics)
                    self.save_file(name, spot_name, all_spot_comms)


commants = Commants()
commants.comm_main()